package com.loto.insurance.mis.web.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.loto.insurance.mis.web.db.mapper.OemInfoMapper;
import com.loto.insurance.mis.web.entity.OemInfo;
import com.loto.insurance.mis.web.entity.ajax.BaseJson;
import com.loto.insurance.mis.web.entity.service.AlipayAccount;
import com.loto.insurance.mis.web.entity.service.OEMConfig;
import com.loto.insurance.mis.web.entity.service.OemPayAccount;
import com.loto.insurance.mis.web.entity.service.PayAccount;
import com.loto.utils.Http;
import com.loto.utils.XML;

@Service
public class OemService {
  private final String PLATFORM = "http://localhost:8090";
  static Logger logger = LogManager.getLogger(OemService.class);
  @Autowired
  OemInfoMapper oemInfoMapper;

  public List<OemInfo> findAllActiveOem() {
	return oemInfoMapper.findAllActiveOem();
  }

  public List<OemInfo> findAllOem(Map<String, Object> params) {
	return oemInfoMapper.findAllOemPager(params);
  }

  public long findAllOemCount(Map<String, Object> params) {
	return oemInfoMapper.findAllOemPagerCount(params);
  }

  public OemInfo findOemById(String oemId) {
	return oemInfoMapper.findOemById(oemId);
  }

  public boolean addOem(OemInfo oemInfo) {
	boolean isOk = false;
	if (oemInfoMapper.insertOem(oemInfo) > 0) {
	  notifyOem(oemInfo.getOemId());
	  isOk = true;
	}
	return isOk;
  }

  public boolean updateOem(OemInfo oemInfo) {
	boolean isOk = false;
	if (oemInfoMapper.updateOem(oemInfo) > 0) {
	  notifyOem(oemInfo.getOemId());
	  isOk = true;
	}
	return isOk;
  }

  private boolean notifyOem(String oemId) {
	boolean isOk = false;
	OemInfo oem = oemInfoMapper.findOemById(oemId);
	if (oem != null) {
	  OEMConfig config = new OEMConfig();
	  config.setOemAcc(oem.getOemAcc());
	  config.setOemDomain(oem.getOemDomain());
	  config.setOemICP(oem.getOemIcp());
	  config.setOemName(oem.getOemName());
	  config.setOemPin(oem.getOemPin());
	  config.setOemPlatform(PLATFORM);
	  config.setOemServiceTel(oem.getOemServiceTel());
	  config.setOemDbConnect(oem.getOemDbConnect());
	  config.setOemDbName(oem.getOemDbName());
	  config.setOemDbPassword(oem.getOemDbPassword());
	  config.setOemDbUsername(oem.getOemDbUsername());
	  OemPayAccount oemPayAccount = new OemPayAccount();
	  AlipayAccount alipay = new AlipayAccount();
	  PayAccount account = new PayAccount();
	  account.setEmail(oem.getOemAlipayEmail());
	  account.setPartner(oem.getOemAlipayPartner());
	  account.setRate(oem.getOemAlipayRate());
	  account.setSecret(oem.getOemAlipaySecret());
	  alipay.setAccount(account);
	  oemPayAccount.setAlipay(alipay);
	  config.setOemPayAccount(oemPayAccount);

	  String xmlRequest = XML.toXML(config, OEMConfig.class);
	  logger.info(xmlRequest);
	  // TODO 通知OEM
	  Map<String, String> paramMap = new HashMap<String, String>();
	  paramMap.put("content", xmlRequest);
	  paramMap.put("acc", config.getOemAcc());
	  paramMap.put("pin", config.getOemPin());
	  try {
		String xmlResponse = Http.doPost("http://" + config.getOemDomain()
		    + "/webservice/updateoeminfo.service", paramMap);
		logger.info(xmlResponse);
		BaseJson response = XML.fromXML(xmlResponse, BaseJson.class);
		if (response == null || !response.isOk()) {
		  logger.error("更新OEM信息[" + config.getOemId() + "-"
			  + config.getOemName() + "]通知失败");
		} else {
		  logger.info("更新OEM信息[" + config.getOemId() + "-"
			  + config.getOemName() + "]通知成功：");
		  isOk = true;
		}
	  } catch (Exception ex) {
		ex.printStackTrace();
		logger.error("更新OEM信息[" + config.getOemId() + "-" + config.getOemName()
		    + "]通知失败：");
	  }
	}
	return isOk;
  }

  public List<OemInfo> findOemByCustId(long custId) {
	return oemInfoMapper.findOemByCustId(custId);
  }
}
